package com.zhoutao123.hephaestus.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.zhoutao123.hephaestus.config.properties.ApplicationProperty;
import com.zhoutao123.hephaestus.config.properties.QiniuProperties;
import com.zhoutao123.hephaestus.object.bussiness.VSysResources;
import com.zhoutao123.hephaestus.object.transport.PageBean;
import com.zhoutao123.hephaestus.repository.entity.SysResources;
import com.zhoutao123.hephaestus.service.AbstractService;
import com.zhoutao123.hephaestus.service.UploadService;
import com.zhoutao123.hephaestus.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class UploadServiceImpl extends AbstractService implements UploadService {

    @Autowired
    private ApplicationProperty applicationProperties;

    public String uploadFileToQiNiuCloud(FileInputStream file, String key) throws IOException {
        QiniuProperties qiniuProperties = applicationProperties.getQiniu();
        // 构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone2());
        // 其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        // 生成上传凭证，然后准备上传
        Auth auth = Auth.create(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey());
        String upToken = auth.uploadToken(qiniuProperties.getBucket());
        Response response = uploadManager.put(file, key, upToken, null, null);
        DefaultPutRet putRet = JacksonUtil.readValue(response.bodyString(), DefaultPutRet.class);
        String returnPath = qiniuProperties.getPath() + "/" + putRet.key;
        return returnPath;
    }

    @Override
    public void saveUploadFileRecord(String fileName, String filePath, Integer fileSize) {
        String[] fileNameSplit = fileName.split("[.]");
        SysResources dao = new SysResources();
        dao.setId(UUID.randomUUID().toString());
        dao.setAvailable(true);
        dao.setCreateTime(new Date());
        dao.setExternal(true);
        dao.setName(fileName);
        dao.setUrl(filePath);
        dao.setSort(fileName);
        dao.setType(fileNameSplit[fileNameSplit.length - 1]);
        sysResourcesRepo.insertSelective(dao);
    }

    @Override
    public VSysResources getFileList(PageBean pageBean) {

        PageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize());
        List<SysResources> sysResources =
                sysResourcesRepo.selectList(
                        criteria1 -> {
                        }, example -> example.setOrderByClause("create_time DESC"));
        PageInfo<SysResources> page = new PageInfo<>(sysResources);
        VSysResources vSysResources = VSysResources.of(page);
        return vSysResources;
    }
}
